package com.kky.pp.util

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import com.custom.base.util.ToastUtil
import com.kky.pp.R


class SystemAppCallUtil {
    /**
     * 发送邮件
     */
    fun checkGmail(context: Activity?, title: String, extraText: String) {
        val packageManager: PackageManager? = context?.packageManager
        val emailIntent1 = Intent(Intent.ACTION_SEND)
        emailIntent1.type = "text/plain"
        val resolveInfoList = packageManager?.queryIntentActivities(emailIntent1, 0)
        var isEmailEnabled = false
        if (resolveInfoList != null) {
            for (resolveInfo in resolveInfoList) {
                if (resolveInfo.activityInfo.packageName.contains("com.google.android.gm")) {
                    isEmailEnabled = true
                    break
                }
            }
        }
        if (isEmailEnabled) {
            sendEmail(context, title, extraText)
            // 系统邮箱应用可用
        } else {
            // 系统邮箱应用被禁用
            ToastUtil.toast("Please set up an email account and send us feedback")
        }
    }

    fun sendEmail(context: Activity?, title: String, extraText: String) {
        try {
            val emailIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"))/*不带附件发送邮件*/
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, title)
            emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(context?.resources?.getString(R.string.email_address)))
            emailIntent.putExtra(Intent.EXTRA_TEXT, extraText) //发送的内容
            context?.startActivity(emailIntent)
        }catch (e :Exception){
            ToastUtil.toast("Please set up an email account and send us feedback")
        }

    }
}